<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../Universal.css">
</head>
<body>
    
    <div id="cnblogs_post_body" class="blogpost-body">
        <h1>List</h1>
    <p>List接口的特点:</p>
    <ul>
    <li>它是一个元素存取有序的集合。例如，存元素的顺序是11、22、33。那么集合中，元素的存储就是按照11、22、33的顺序完成的。</li>
    <li>它是一个带有索引的集合，通过索引就可以精确的操作集合中的元素（与数组的索引是一个道理）。</li>
    <li>集合中可以有重复的元素，通过元素的equals方法，来比较是否为重复的元素。</li>
    </ul>
    <p>List接口的常用子类有：</p>
    <ul>
    <li>ArrayList集合</li>
    <li>LinkedList集合</li>
    </ul>
    <p>List接口的特有方法(带索引的方法)<br>1、增加元素方法</p>
    <ul>
    <li>add(Object e)：向集合末尾处，添加指定的元素 </li>
    <li>add(int index, Object e)   向集合指定索引处，添加指定的元素，原有元素依次后移</li>
    
    
    
    
    
    </ul>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
    * add(int index, E)
    * 将元素插入到列表的指定索引上
    * 带有索引的操作,防止越界问题
    * java.lang.IndexOutOfBoundsException
    * ArrayIndexOutOfBoundsException
    * StringIndexOutOfBoundsException
    </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> function(){
            List</span>&lt;String&gt; list=<span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;String&gt;<span style="color: rgba(0, 0, 0, 1)">();
            list.add(</span>"abc1"<span style="color: rgba(0, 0, 0, 1)">);
            list.add(</span>"abc2"<span style="color: rgba(0, 0, 0, 1)">);
            list.add(</span>"abc3"<span style="color: rgba(0, 0, 0, 1)">);
            list.add(</span>"abc4"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(list);
    
            list.add(</span>1,"itcast"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(list);
    }</span></pre>
    </div>
    <p>2、删除元素</p>
    <ul>
    <li>remove(Object e)：将指定元素对象，从集合中删除，返回值为被删除的元素</li>
    <li>remove(int index)：将指定索引处的元素，从集合中删除，返回值为被删除的元素</li>
    </ul>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
    * E remove(int index)
    * 移除指定索引上的元素
    * 返回被删除之前的元素
    </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> function_1(){
            List</span>&lt;Double&gt; list = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;Double&gt;<span style="color: rgba(0, 0, 0, 1)">();
            list.add(</span>1.1<span style="color: rgba(0, 0, 0, 1)">);
            list.add(</span>1.2<span style="color: rgba(0, 0, 0, 1)">);
            list.add(</span>1.3<span style="color: rgba(0, 0, 0, 1)">);
            list.add(</span>1.4<span style="color: rgba(0, 0, 0, 1)">);
    
            Double d </span>= list.remove(0<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(d);
            System.out.println(list);
    } </span></pre>
    </div>
    <pre><span>3、替换元素方法<br></span></pre>
    <pre><span>set(int<span> index, Object e)：将指定索引处的元素，替换成指定的元素，返回值为替换前的元素</span></span></pre>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
    * E set(int index, E)
    * 修改指定索引上的元素
    * 返回被修改之前的元素
    </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> function_2(){
            List</span>&lt;Integer&gt; list = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;Integer&gt;<span style="color: rgba(0, 0, 0, 1)">();
            list.add(</span>1<span style="color: rgba(0, 0, 0, 1)">);
            list.add(</span>2<span style="color: rgba(0, 0, 0, 1)">);
            list.add(</span>3<span style="color: rgba(0, 0, 0, 1)">);
            list.add(</span>4<span style="color: rgba(0, 0, 0, 1)">);
    
            Integer i </span>= list.set(0, 5<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(i);
            System.out.println(list);
    }</span></pre>
    </div>
    <p>查询元素方法</p>
    <p>get(int index)：获取指定索引处的元素，并返回该元素</p>
    <h2>迭代器的并发修改异常</h2>
    <p>迭代器的并发修改异常 java.util.ConcurrentModificationException就是在遍历的过程中,使用了集合方法修改了集合的长度,这是不允许的。</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.ArrayList;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.Iterator;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.List;
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class </span>ListDemo1&nbsp;{</pre>
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            List</span>&lt;String&gt; list = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;String&gt;<span style="color: rgba(0, 0, 0, 1)">();
            list.add(</span>"abc1"<span style="color: rgba(0, 0, 0, 1)">);
            list.add(</span>"abc2"<span style="color: rgba(0, 0, 0, 1)">);
            list.add(</span>"abc3"<span style="color: rgba(0, 0, 0, 1)">);
            list.add(</span>"abc4"<span style="color: rgba(0, 0, 0, 1)">);
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">对集合使用迭代器进行获取,获取时候判断集合中是否存在 "abc3"对象
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果有,添加一个元素 "ABC3"</span>
            Iterator&lt;String&gt; it =<span style="color: rgba(0, 0, 0, 1)"> list.iterator();
            </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)">(it.hasNext()){
                String s </span>=<span style="color: rgba(0, 0, 0, 1)"> it.next();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">对获取出的元素s,进行判断,是不是有"abc3"</span>
                <span style="color: rgba(0, 0, 255, 1)">if</span>(s.equals("abc3"<span style="color: rgba(0, 0, 0, 1)">)){
                    list.add(</span>"ABC3"<span style="color: rgba(0, 0, 0, 1)">);
                }
                System.out.println(s);
            }
        }
    }</span></pre>
    </div>
    <p>运行结果：</p>
    <div class="cnblogs_code">
    <pre>Exception in thread "main"<span style="color: rgba(0, 0, 0, 1)"> abc1
    abc2
    abc3
    java.util.ConcurrentModificationException
        at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:</span>901<span style="color: rgba(0, 0, 0, 1)">)
        at java.util.ArrayList$Itr.next(ArrayList.java:</span>851<span style="color: rgba(0, 0, 0, 1)">)
        at ArrayListDemo.main(ArrayListDemo.java:</span>17)</pre>
    </div>
    <p>运行上述代码发生了错误 java.util.ConcurrentModificationException这是什么原因呢？在迭代过程中，使用了集合的方法对元素进行操作。导致迭代器并不知道集合中的变化，容易引发数据的不确定性。</p>
    <p>并发修改异常解决办法：在迭代时，不要使用集合的方法操作元素。或者通过ListIterator迭代器操作元素是可以的，ListIterator的出现，解决了使用Iterator迭代过程中可能会发生的错误情况。</p>
    <h2>数据的存储结构</h2>
    <p>&nbsp;栈 结 构：后进先出/先进后出(手枪弹夹) FILO (first in last out)</p>
    <p>队列结构：先进先出/后进后出(银行排队) FIFO(first in first out)</p>
    <p>数组结构：查询快，通过索引快速找到元素；增删慢:每次增删都需要开辟新的数组,将老数组中的元素拷贝到新数组中；开辟新数组耗费资源。</p>
    <p>链表结构：查询慢，每次都需要从链头或者链尾找起；增删快，只需要修改元素记录的下个元素的地址值即可不需要移动大量元素</p>
    <h3>ArrayList集合的自身特点</h3>
    <p>底层采用的是数组结构</p>
    <div class="cnblogs_code">
    <pre>ArrayList al=<span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建了一个长度为0的Object类型数组</span>
    al.add("abc");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">底层会创建一个长度为10的Object数组 Object[] obj=new Object[10]
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">obj[0]="abc"
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果添加的元素的超过10个,底层会开辟一个1.5*10的长度的新数组
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">把原数组中的元素拷贝到新数组,再把最后一个元素添加到新数组中</span>
    <span style="color: rgba(0, 0, 0, 1)">//原数组:a b c d e f g h k l
    //添加m:a b c d e f g h k l m </span><span style="color: rgba(0, 0, 255, 1)">null</span> <span style="color: rgba(0, 0, 255, 1)">null</span> <span style="color: rgba(0, 0, 255, 1)">null</span> <span style="color: rgba(0, 0, 255, 1)">null</span></pre>
    </div>
    <h3>LinkedList集合的自身特点</h3>
    <p>底层采用链表结构,每次查询都要从链头或链尾找起,查询相对数组较慢，但是删除直接修改元素记录的地址值即可,不要大量移动元素<br>LinkedList的索引决定是从链头开始找还是从链尾开始找，如果该元素小于元素长度一半,从链头开始找起,如果大于元素长度的一半,则从链尾找起</p>
    <p>LinkedList特有方法:获取,添加,删除</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
    * LinkedList 链表集合的特有功能
    * 自身特点: 链表底层实现,查询慢,增删快
    * 
    * 子类的特有功能,不能多态调用
    </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> LinkedListDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            function_3();
        }
        </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
        * E removeFirst() 移除并返回链表的开头
        * E removeLast() 移除并返回链表的结尾
        </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> function_3(){
            LinkedList</span>&lt;String&gt; link = <span style="color: rgba(0, 0, 255, 1)">new</span> LinkedList&lt;String&gt;<span style="color: rgba(0, 0, 0, 1)">();
            link.add(</span>"1"<span style="color: rgba(0, 0, 0, 1)">);
            link.add(</span>"2"<span style="color: rgba(0, 0, 0, 1)">);
            link.add(</span>"3"<span style="color: rgba(0, 0, 0, 1)">);
            link.add(</span>"4"<span style="color: rgba(0, 0, 0, 1)">);
    
            String first </span>=<span style="color: rgba(0, 0, 0, 1)"> link.removeFirst();
            String last </span>=<span style="color: rgba(0, 0, 0, 1)"> link.removeLast();
            System.out.println(first);
            System.out.println(last);
    
            System.out.println(link);
        }
    
        </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
        * E getFirst() 获取链表的开头
        * E getLast() 获取链表的结尾
        </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> function_2(){
            LinkedList</span>&lt;String&gt; link = <span style="color: rgba(0, 0, 255, 1)">new</span> LinkedList&lt;String&gt;<span style="color: rgba(0, 0, 0, 1)">();
            link.add(</span>"1"<span style="color: rgba(0, 0, 0, 1)">);
            link.add(</span>"2"<span style="color: rgba(0, 0, 0, 1)">);
            link.add(</span>"3"<span style="color: rgba(0, 0, 0, 1)">);
            link.add(</span>"4"<span style="color: rgba(0, 0, 0, 1)">);
    
            </span><span style="color: rgba(0, 0, 255, 1)">if</span>(!<span style="color: rgba(0, 0, 0, 1)">link.isEmpty()){
                String first </span>=<span style="color: rgba(0, 0, 0, 1)"> link.getFirst();
                String last </span>=<span style="color: rgba(0, 0, 0, 1)"> link.getLast();
                System.out.println(first);
                System.out.println(last);
            }
        }
    
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> function_1(){
            LinkedList</span>&lt;String&gt; link = <span style="color: rgba(0, 0, 255, 1)">new</span> LinkedList&lt;String&gt;<span style="color: rgba(0, 0, 0, 1)">();
            link.addLast(</span>"a"<span style="color: rgba(0, 0, 0, 1)">);
            link.addLast(</span>"b"<span style="color: rgba(0, 0, 0, 1)">);
            link.addLast(</span>"c"<span style="color: rgba(0, 0, 0, 1)">);
            link.addLast(</span>"d"<span style="color: rgba(0, 0, 0, 1)">);
    
            link.addFirst(</span>"1"<span style="color: rgba(0, 0, 0, 1)">);
            link.addFirst(</span>"2"<span style="color: rgba(0, 0, 0, 1)">);
            link.addFirst(</span>"3"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(link);
        }
    
        </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
        * addFirst(E) 添加到链表的开头
        * addLast(E) 添加到链表的结尾
        </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> function(){
            LinkedList</span>&lt;String&gt; link = <span style="color: rgba(0, 0, 255, 1)">new</span> LinkedList&lt;String&gt;<span style="color: rgba(0, 0, 0, 1)">();
    
            link.addLast(</span>"heima"<span style="color: rgba(0, 0, 0, 1)">);
    
            link.add(</span>"abc"<span style="color: rgba(0, 0, 0, 1)">);
            link.add(</span>"bcd"<span style="color: rgba(0, 0, 0, 1)">);
    
            link.addFirst(</span>"itcast"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(link);
        }
    }</span></pre>
    </div>
    <h2>Vector</h2>
    <p>Vector集合数据存储的结构是数组结构，为JDK中最早提供的集合,它是线程同步的。</p>
    <p>Vector中提供了一个独特的取出方式，就是枚举Enumeration，它其实就是早期的迭代器。</p>
    <p> 此接口Enumeration的功能与 Iterator 接口的功能是类似的。<br>       Vector集合已被ArrayList替代。枚举Enumeration已被迭代器Iterator替代。</p>
    <h1><sup>Set</sup></h1>
    <h3>Set接口的特点 </h3>
    <p>它是个不包含重复元素的集合。<br>Set集合取出元素的方式可以采用：迭代器、增强for。<br>Set集合有多个子类，这里我们介绍其中的HashSet、LinkedHashSet这两个集合。&nbsp;</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
    * Set接口,特点不重复元素,没索引
    * 
    * Set接口的实现类,HashSet (哈希表)
    * 特点: 无序集合,存储和取出的顺序不同,没有索引,不存储重复元素
    * 代码的编写上,和ArrayList完全一致
    </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> HashSetDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            Set</span>&lt;String&gt; set = <span style="color: rgba(0, 0, 255, 1)">new</span> HashSet&lt;String&gt;<span style="color: rgba(0, 0, 0, 1)">();
            set.add(</span>"cn"<span style="color: rgba(0, 0, 0, 1)">);
            set.add(</span>"heima"<span style="color: rgba(0, 0, 0, 1)">);
            set.add(</span>"java"<span style="color: rgba(0, 0, 0, 1)">);
            set.add(</span>"java"<span style="color: rgba(0, 0, 0, 1)">);
            set.add(</span>"itcast"<span style="color: rgba(0, 0, 0, 1)">);
    
            Iterator</span>&lt;String&gt; it =<span style="color: rgba(0, 0, 0, 1)"> set.iterator();
            </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)">(it.hasNext()){
                System.out.println(it.next());
            }
            System.out.println(</span>"=============="<span style="color: rgba(0, 0, 0, 1)">);
    
            </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)">(String s : set){
                System.out.println(s);
            }
        }
    }</span></pre>
    </div>
    <h3>哈希表的数据结构</h3>
    <p>加载因子:表中填入的记录数/哈希表的长度</p>
    <p>例如:加载因子是0.75 代表：数组中的16个位置,其中存入16*0.75=12个元素</p>
    <p> 如果在存入第十三个(&gt;12)元素,导致存储链子过长,会降低哈希表的性能,那么此时会扩充哈希表(在哈希),底层会开辟一个长度为原长度2倍的数组,把老元素拷贝到新数组中,再把新元素添加数组中<br>        当存入元素数量&gt;哈希表长度*加载因子,就要扩容,因此加载因子决定扩容时机</p>
    <h3>字符串对象的哈希值&nbsp;</h3>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
     *  对象的哈希值,普通的十进制整数
     *  父类Object,方法 public int hashCode() 计算结果int整数
     </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> HashDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            String s1 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> String("abc"<span style="color: rgba(0, 0, 0, 1)">);
            String s2 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> String("abc"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(s1.hashCode());</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">96354</span>
            System.out.println(s2.hashCode());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">96354</span>
    <span style="color: rgba(0, 0, 0, 1)">
            System.out.println(</span>"重地".hashCode());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1179395</span>
            System.out.println("通话".hashCode());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1179395</span>
    <span style="color: rgba(0, 0, 0, 1)">    }
    }</span></pre>
    </div>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">String类重写hashCode()方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">字符串都会存储在底层的value数组中{'a','b','c'}</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> hashCode() {
        </span><span style="color: rgba(0, 0, 255, 1)">int</span> h =<span style="color: rgba(0, 0, 0, 1)"> hash;
        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (h == 0 &amp;&amp; value.length &gt; 0<span style="color: rgba(0, 0, 0, 1)">) {
            </span><span style="color: rgba(0, 0, 255, 1)">char</span> val[] =<span style="color: rgba(0, 0, 0, 1)"> value;
    
            </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; value.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
                h </span>= 31 * h +<span style="color: rgba(0, 0, 0, 1)"> val[i];
            }
            hash </span>=<span style="color: rgba(0, 0, 0, 1)"> h;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> h;
    }</span></pre>
    </div>
    <p><em><img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170714113420212-1243396718.jpg" alt=""><br></em></p>
    <h3>哈希表的存储过程 </h3>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
        HashSet</span>&lt;String&gt; set = <span style="color: rgba(0, 0, 255, 1)">new</span> HashSet&lt;String&gt;<span style="color: rgba(0, 0, 0, 1)">();
        set.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> String("abc"<span style="color: rgba(0, 0, 0, 1)">));
        set.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> String("abc"<span style="color: rgba(0, 0, 0, 1)">));
        set.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> String("bbc"<span style="color: rgba(0, 0, 0, 1)">));
        set.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> String("bbc"<span style="color: rgba(0, 0, 0, 1)">));
        System.out.println(set);
    }</span></pre>
    </div>
    <p>存取原理，每存入一个新的元素都要走以下三步:</p>
    <p>1.首先调用本类的hashCode()方法算出哈希值</p>
    <p>2.在容器中找是否与新元素哈希值相同的老元素,如果没有直接存入；如果有转到第三步</p>
    <p>3.新元素会与该索引位置下的老元素利用equals方法一一对比，一旦新元素.equals(老元素)返回true,停止对比,说明重复,不再存入，如果与该索引位置下的老元素都通过equals方法对比返回false,说明没有重复,存入。hashCode()返回一个是十进制的值,这个值叫做哈希值,不同的类都会重写Object类的hashCode()方法,底层有不同的生成哈希值的算法,下面介绍如何通过hashCode()方法和equals()方法保证存入HashSet的值唯一的。</p>
    <p><img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170714135155618-1099709226.jpg" alt=""></p>
    <h3>哈希表的存储自定义对象</h3>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
    * HashSet集合的自身特点:
    * 底层数据结构,哈希表
    * 存储,取出都比较快
    * 线程不安全,运行速度快
    </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> HashSetDemo1 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将Person对象中的姓名,年龄,相同数据,看作同一个对象
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">判断对象是否重复,依赖对象自己的方法 hashCode,equals</span>
            HashSet&lt;Person&gt; setPerson = <span style="color: rgba(0, 0, 255, 1)">new</span> HashSet&lt;Person&gt;<span style="color: rgba(0, 0, 0, 1)">();
            setPerson.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Person("a",11<span style="color: rgba(0, 0, 0, 1)">));
            setPerson.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Person("b",10<span style="color: rgba(0, 0, 0, 1)">));
            setPerson.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Person("b",10<span style="color: rgba(0, 0, 0, 1)">));
            setPerson.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Person("c",25<span style="color: rgba(0, 0, 0, 1)">));
            setPerson.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Person("d",19<span style="color: rgba(0, 0, 0, 1)">));
            setPerson.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Person("e",17));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">每个对象的地址值都不同,调用Obejct类的hashCode方法返回不同哈希值,直接存入</span>
    <span style="color: rgba(0, 0, 0, 1)">        System.out.println(setPerson);
        }
    }</span></pre>
    </div>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Person {
        </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String name;
        </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age;
    
        </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
        * 没有做重写父类,每次运行结果都是不同整数
        * 如果子类重写父类的方法,哈希值,自定义的
        * 存储到HashSet集合的依据
        * 
        * 尽可能让不同的属性值产生不同的哈希值,这样就不用再调用equals方法去比较属性
        *
        </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> hashCode(){
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> name.hashCode()+age*55<span style="color: rgba(0, 0, 0, 1)">;
        }
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">方法equals重写父类,保证和父类相同
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public boolean equals(Object obj){}</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> equals(Object obj){
            </span><span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(0, 0, 255, 1)">this</span> ==<span style="color: rgba(0, 0, 0, 1)"> obj)
                </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">if</span>(obj == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
                </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">if</span>(obj <span style="color: rgba(0, 0, 255, 1)">instanceof</span><span style="color: rgba(0, 0, 0, 1)"> Person){
                Person p </span>=<span style="color: rgba(0, 0, 0, 1)"> (Person)obj;
                </span><span style="color: rgba(0, 0, 255, 1)">return</span> name.equals(p.name) &amp;&amp; age==<span style="color: rgba(0, 0, 0, 1)">p.age;
            }
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
        }
    
        </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getName() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> name;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setName(String name) {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getAge() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> age;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setAge(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age) {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.age =<span style="color: rgba(0, 0, 0, 1)"> age;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> Person(String name, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age) {
            </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">();
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name;
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.age =<span style="color: rgba(0, 0, 0, 1)"> age;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Person(){}
    
        </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String toString(){
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> name+".."+<span style="color: rgba(0, 0, 0, 1)">age;
        }
    }</span></pre>
    </div>
    <h3><img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170714135436072-272023180.jpg" alt=""></h3>
    <h3>LinkedHashSet集合</h3>
    <p>LinkedHashSet 基于链表的哈希表实现，继承自HashSet。</p>
    <p>LinkedHashSet 自身特性：</p>
    <ul>
    <li>具有顺序,存储和取出的顺序相同的；</li>
    <li>线程不安全的集合,运行速度块</li>
    </ul>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> LinkedHashSetDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            LinkedHashSet</span>&lt;Integer&gt; link = <span style="color: rgba(0, 0, 255, 1)">new</span> LinkedHashSet&lt;Integer&gt;<span style="color: rgba(0, 0, 0, 1)">();
            link.add(</span>123<span style="color: rgba(0, 0, 0, 1)">);
            link.add(</span>44<span style="color: rgba(0, 0, 0, 1)">);
            link.add(</span>33<span style="color: rgba(0, 0, 0, 1)">);
            link.add(</span>33<span style="color: rgba(0, 0, 0, 1)">);
            link.add(</span>66<span style="color: rgba(0, 0, 0, 1)">);
            link.add(</span>11<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(link);
        }
    }</span></pre>
    </div>
    <h3>ArrayList,HashSet判断对象是否重复的原因</h3>
    <p>ArrayList的contains方法原理:底层依赖于equals方法，ArrayList的contains方法调用时，会使用传入元素的equals方法依次与集合中的旧元素所比较，从而根据返回的布尔值判断是否有重复元素。</p>
    <p>此时，当ArrayList存放自定义类型时，由于自定义类型在未重写equals方法前，判断是否重复的依据是地址值，所以如果想根据内容判断是否为重复元素，需要重写元素的equals方法。HashSet的add()方法和contains方法()底层都依赖 hashCode()方法与equals方法()。</p>
    <p>Set集合不能存放重复元素，其添加方法在添加时会判断是否有重复元素，有重复不添加，没重复则添加。</p>
    <p>HashSet集合由于是无序的，其判断唯一的依据是元素类型的hashCode与equals方法的返回结果。规则如下：</p>
    <p>先判断新元素与集合内已经有的旧元素的HashCode值，如果不同，说明是不同元素，添加到集合。如果相同，再判断equals比较结果。返回true则相同元素；返回false则不同元素，添加到集合。</p>
    <p>所以，使用HashSet存储自定义类型，如果没有重写该类的hashCode与equals方法，则判断重复时，使用的是地址值，如果想通过内容比较元素是否相同，需要重写该元素类的hashcode与equals方法。</p>
    <h3>hashCode和equals的面试题</h3>
    <p>两个对象 Person p1 p2，如果两个对象的哈希值相同 p1.hashCode()==p2.hashCode()，两个对象的equals一定返回true吗？ 即p1.equals(p2) 一定是true吗？</p>
    <p>正确答案:不一定</p>
    <p>如果两个对象的equals方法返回true,p1.equals(p2)==true，两个对象的哈希值一定相同吗？</p>
    <p>正确答案: 一定</p>
    <p>Java对于eqauls方法和hashCode方法是这样规定的： &nbsp; &nbsp;&nbsp;</p>
    <ul>
    <li>1.如果两个对象相同，那么它们的hashCode值一定要相同；</li>
    <li>2.如果两个对象的hashCode相同，它们并不一定相同（这里说的对象相同指的是用eqauls方法比较）。如不按要求去做了，会发现相同的对象可以出现在Set集合中，同时，增加新元素的效率会大大下降。</li>
    <li>3.equals()相等的两个对象，hashcode()一定相等；equals()不相等的两个对象，却并不能证明他们的hashcode()不相等。换句话说，equals()方法不相等的两个对象，hashcode()有可能相等（我的理解是由于哈希码在生成的时候产生冲突造成的）。反过来，hashcode()不等，一定能推出equals()也不等；hashcode()相等，equals()可能相等，也可能不等。</li>
    </ul>
    <h3>&nbsp;归纳总结</h3>
    <p>&nbsp;<img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170714112051572-1569523780.png" alt=""></p>
    </div>
    
</body>
</html>